package com.errol.batchprocessor.payroll.advisorimpl.other;

import com.renjia.enums.AgreementSigningStatusEnum;
import com.renjia.enums.PayTipTypeEnum;
import com.renjia.service.generic.support.signing.SigningAgainSupportService;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeePiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeSinglePayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifyBatchStartAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifySingleProcessAdvisor;
import com.renjia.service.module.payroll.advisorimpl.PayrollAdvisorTypeEnum;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import com.renjia.service.module.payrollopenapi.po.OpenApiPayrollDetailPO;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 同程生活扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/08
 * @since v5.0.6
 **/
@Service
public class TcShAdvisorImpl implements
        PayrollUploadCreateAdvisor,
        PayrollVerifyBatchStartAdvisor,
        PayrollVerifySingleProcessAdvisor {

    private final SigningAgainSupportService signingAgainSupportService;

    public TcShAdvisorImpl(SigningAgainSupportService signingAgainSupportService) {
        this.signingAgainSupportService = signingAgainSupportService;
    }

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return PayrollAdvisorTypeEnum.TC_SH == advisorPayload.getPayrollAdvisorTypeEnum();
    }

    @Override
    public int priority() {
        return 202;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // 覆盖默认的代发类型
        batchPayload.getBatchEntity().setPayTip(true);
        batchPayload.getBatchEntity().setPayTipType(PayTipTypeEnum.API.getCode());
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        // v5.0.1 同程生活发放，记录合作方用户id
        singlePayload.getBusPayrollRecordDetail().setPartnerPayrollDetailId(((OpenApiPayrollDetailPO) singlePayload.getPayrollDetailPo()).getPartnerPayrollDetailId());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        singlePayload.getErrMsgList().add("同程生活接口发放不允许编辑");
        return false;
    }

    @Override
    public boolean doPrepareOnBatchStartAtVerifyStage(@NonNull PayrollEmployeeBatchPayload batchPayload) {
        // 无视手机号重复时，但不写入重复手机号
        batchPayload.setIgnoreMobileRepeated(true);
        return true;
    }

    @Override
    public boolean doExecuteOnEmployeeInfoSavedAtVerifyStage(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload) {
        // 人员导入成功后（包括生成待签协议），如果该用户没有已签协议，则自动补签
        if (singlePayload.getCoreAgreementDetail() != null &&
                !AgreementSigningStatusEnum.SIGNED.getCode().toString().equals(singlePayload.getCoreAgreementDetail().getContractStatus())) {
            signingAgainSupportService.addSigningAgain(singlePayload.getCoreEmployeeCustomer(), batchPayload.getProtocolAndSignatureBO().getCoreAgreementTemplate(),
                    batchPayload.getProtocolAndSignatureBO().getSignatureFile(), null, null);
        }
        return true;
    }
}
